package main

import (
	//"encoding/json"
	"github.com/playnb/mustang/global"
	"github.com/playnb/mustang/log"
	"github.com/playnb/mustang/nosql"
	"github.com/playnb/mustang/sqldb"
	//"github.com/playnb/mustang/utils"
	"net/http"

	"github.com/playnb/mustang/weixin"
	//"net/url"
	//"time"
	_ "net/http/pprof"
)

func main() {
	defer log.Close()

	go func() {
		http.ListenAndServe(":3003", nil)
	}()

	nosql.InitRedis(global.C.RedisUrl, global.C.RedisPin, 0)
	sqldb.Init()
	mux := http.NewServeMux()
	weixin.InitWeiXin(&weixin.WeiXinConfig{AppID: global.C.AppID,
		AppSecret:       global.C.AppSecret,
		OwnerID:         global.C.OwnerID,
		ProcessMsg:      nil,
		UserAuthorized:  UserAuthorized,
		ServiceToken:    global.C.ServiceToken,
		MyServiceDomain: global.C.ServiceDomain,
		AuthRedirectURL: "http://" + global.C.ServiceDomain + "/wx/authorize_redirect"}, mux)

	//weixin.RegistAuthRedirectUrl(global.AppName, "http://"+global.ServiceDomain+"/app/"+global.AppName)

	log.Trace("当前目录: %s", getCurrentDirectory())
	//静态文件
	mux.HandleFunc("/apps/", handleStatic)
	mux.HandleFunc("/", handleRoot)
	//mux.HandleFunc("/ResetAll", handleResetAll)
	mux.HandleFunc("/ads", handleAds)

	mux.HandleFunc("/ResetByOpenID", handleResetByOpenID)
	mux.HandleFunc("/DefaultData", handleDefaultData)
	mux.HandleFunc("/resetShareFirends", resetShareFirends)
	mux.HandleFunc("/resetShareTimeline", resetShareTimeline)
	mux.HandleFunc("/resetBestScore", resetBestScore)

	registMonitor(mux)

	mux.HandleFunc("/tools/upload/", handleUpload)

	log.Trace("绑定端口: " + global.C.HttpPort)
	if err := http.ListenAndServe(":"+global.C.HttpPort, mux); err != nil {
		log.Error(err.Error())
	}
	//if err := http.ListenAndServeTLS(":3000", baseDir+"https/cert.pem", baseDir+"https/key.pem", mux); err != nil {
	//	log.Error(err.Error())
	//}
	return
}
